package Hot100.twoSearch;

public class search {
    public static void main(String[] args) {

    }
    public int search(int[] nums, int target) {
        int n = nums.length;
        if(n == 0){
            return -1;
        }
        if(n == 1){
            return nums[0] == target? 0:-1;
        }

        //无论怎么二分，总有一半是有序的
        int low = 0, high = n - 1;
        while(low <= high){
            int mid = (high - low) / 2 + low;
            if(nums[mid] == target){
                return mid;
            }
            //
            if(nums[low] <= nums[mid]){
                if(nums[low] <= target && target < nums[mid]){
                    high = mid - 1;
                }
                else {
                    low = mid + 1;
                }
            }else {
                if(nums[mid] < target && target <= nums[high]){
                    low = mid + 1;
                }
                else {
                    high = mid - 1;
                }
            }
        }
        return -1;
    }
}
